package module.main.view.mainInterface
{
	
	import code.ui.SimpleToolTips;
	import code.utils.MaterialUtil;
	
	import com.darwin.darwinDll.Dll;
	import com.darwin.darwinUI.containers.ContainerBase;
	import com.darwin.darwinUI.controls.Label;
	import com.darwin.darwinUI.core.UIComponent;
	import com.darwin.darwinUI.general.TextFieldFactory;
	import com.darwin.darwinUI.manager.ToolTipManager;
	import com.darwin.darwinUI.manager.UIManager;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import module.main.MainConfig;
	import module.main.view.MainView;
	import module.main.view.ResourceConst;
	

	/**
	 *玩家等级以及领地等级经验面板 
	 * @author jichang
	 * 
	 */
	public class PlayGradePlane extends ContainerBase
	{
		/**
		 *玩家个人等级背景框 
		 */		
		private var playerExperiencePlane:Sprite;
		/**
		 *玩家领地等级经验框 
		 */		
		private var playerManorExperiencePlane:Sprite;
		/**
		 *玩家个人等级ICON 
		 */		
		private var yellowStarIcon:UIComponent;
		/**
		 *玩家个人等级经验条 
		 */		
		private var yellowExperienceStrip:Sprite;
		/**
		 *玩家领地等级经验条 
		 */		
		private var blueExperienceStrip:Sprite;
		/**
		 *玩家领地等级ICON 
		 */		
		private var playerManorLevelIcon:Sprite;
		private var _playerLevelTxt:Label;
		private var _playerManorLevelTxt:Label;
		private var _playerExperienceTxt:Label;
		private var _playerBlueExperienceTxt:Label;
		private var uicommand:UIComponent;
		public function PlayGradePlane()
		{
			uicommand = new UIComponent();
			playerExperiencePlane = MaterialUtil.getPreloadMaterial("main",ResourceConst.PLAYER_EXPERIENCE_PLANE) as Sprite;
			playerManorExperiencePlane = MaterialUtil.getPreloadMaterial("main",ResourceConst.PLAYER_EXPERIENCE_PLANE) as Sprite;
			yellowStarIcon = new UIComponent;
			yellowStarIcon.backgroundSkin = ResourceConst.GRADE_ICON;
			yellowStarIcon.backgroundAuto = true;
			yellowExperienceStrip = MaterialUtil.getPreloadMaterial("main",ResourceConst.YELLOW_EXPERIENCE_STRIP) as Sprite;
			blueExperienceStrip = MaterialUtil.getPreloadMaterial("main",ResourceConst.BLUE_EXPERIENCE_STRIP) as Sprite;
			playerManorLevelIcon = MaterialUtil.getPreloadMaterial("main",ResourceConst.PLAYER_MANOR_LEVEL_ICON) as Sprite;
			yellowStarIcon.width = yellowStarIcon.width - 5;
			yellowStarIcon.height = yellowStarIcon.height - 4;
			
			_playerLevelTxt = TextFieldFactory.getInstance().getLabel("mainInterface_resource_number");
			_playerManorLevelTxt = TextFieldFactory.getInstance().getLabel("mainInterface_resource_number");
			_playerExperienceTxt = TextFieldFactory.getInstance().getLabel("mainInterface_resource_number");
			_playerBlueExperienceTxt = TextFieldFactory.getInstance().getLabel("mainInterface_resource_number");

			
			_playerLevelTxt.mouseEnabled = false;
			_playerManorLevelTxt.mouseEnabled = false;
			_playerExperienceTxt.mouseEnabled = false;
			_playerBlueExperienceTxt.mouseEnabled = false;
			
			this.width = playerExperiencePlane.width;
			this.height = playerExperiencePlane.height * 2;
			uicommand.width =  playerExperiencePlane.width;
			uicommand.height = playerExperiencePlane.height * 2;
			this.addItem(uicommand);
			init();
			
//			yellowStarIcon.mouseChildren = false;
			playerManorLevelIcon.mouseChildren = false;
			yellowExperienceStrip.mouseChildren = false;
			blueExperienceStrip.mouseChildren = false;
			yellowStarIcon.addEventListener(MouseEvent.MOUSE_MOVE,mousemoveHandler);
			playerManorLevelIcon.addEventListener(MouseEvent.MOUSE_MOVE,mousemoveHandler);
			yellowExperienceStrip.addEventListener(MouseEvent.MOUSE_MOVE,mousemoveHandler);
			blueExperienceStrip.addEventListener(MouseEvent.MOUSE_MOVE,mousemoveHandler);

			updateUI();
		}
		private function mousemoveHandler(e:MouseEvent):void
		{
			if(MainView.getInstance().playerObj)
				{
				switch(e.target){
					case yellowStarIcon:
						UIManager.getToolTipManager().showTips(SimpleToolTips,e.target,"领主升级还需<br>"+MainConfig.getNextDiffPrestige(int(MainView.getInstance().playerObj["prosperity"]))+"声望，声望可<br>以通过战斗获得",ToolTipManager.DOWN);
						break;
					case playerManorLevelIcon:
						UIManager.getToolTipManager().showTips(SimpleToolTips,e.target,"领地升级还需要<br>"+MainConfig.getNextDiffProsperity(int(MainView.getInstance().playerObj["reputation"]))+"繁荣度<br>，繁荣度可以通过<br>建造各种建筑<br>获得",ToolTipManager.DOWN);
						break;
					case yellowExperienceStrip:
						UIManager.getToolTipManager().showTips(SimpleToolTips,e.target,MainView.getInstance().playerObj["prosperity"].toString() + " / " + MainConfig.getNextLevelPrestige(MainView.getInstance().playerObj["prosperity"]),ToolTipManager.DOWN);
						break;
					case blueExperienceStrip:
						UIManager.getToolTipManager().showTips(SimpleToolTips,e.target,MainView.getInstance().playerObj["reputation"].toString() + " / " + MainConfig.getNextLevelProsperity(MainView.getInstance().playerObj["reputation"]),ToolTipManager.DOWN);
						break;
					default:
						break;
				}
			}
		}

		public function updateUI():void{
			if(MainView.getInstance().playerObj)
			{
				_playerLevelTxt.text = MainView.getInstance().playerObj.userLevel.toString(); 
				_playerManorLevelTxt.text = MainView.getInstance().playerObj.landLevel.toString()
				_playerExperienceTxt.text = MainView.getInstance().playerObj.prosperity.toString()
				_playerBlueExperienceTxt.text = MainView.getInstance().playerObj.reputation.toString()
			}
			else
			{
				_playerLevelTxt.text = "100";
				_playerManorLevelTxt.text = "100";
				_playerExperienceTxt.text = "99999";
				_playerBlueExperienceTxt.text = "99999";
			}
			
			_playerLevelTxt.x = 20 - _playerLevelTxt.width/2;
			_playerLevelTxt.y = 10;
			_playerManorLevelTxt.x = 20 - _playerManorLevelTxt.width/2;
		}
		

		private function init():void
		{
			setPlayerExperiencePlane();
			setYellowStarIcon();
			setYellowExperienceStrip();
			setPlayerManorLevelIcon();
			setBlueExperienceStrip();
		}
		private function setPlayerExperiencePlane():void
		{
			playerExperiencePlane.x = 0;
			playerExperiencePlane.y = 0;
			playerManorExperiencePlane.x = 0;
			playerManorExperiencePlane.y = playerExperiencePlane.height - 10 ;
			uicommand.addChild(playerExperiencePlane);
			uicommand.addChild(playerManorExperiencePlane);
		}
		private function setYellowStarIcon():void
		{
			yellowStarIcon.x = 9;
			yellowStarIcon.y = 9;
			uicommand.addChild(yellowStarIcon);
		}
		private function setYellowExperienceStrip():void
		{
			yellowExperienceStrip.x = yellowStarIcon.x + yellowStarIcon.background.width;
			yellowExperienceStrip.y = yellowStarIcon.y;
			_playerExperienceTxt.x = yellowExperienceStrip.x + 25;
			_playerExperienceTxt.y = yellowExperienceStrip.y;
			uicommand.addChild(yellowExperienceStrip);
			uicommand.addChild(_playerExperienceTxt);
		}
		private function setBlueExperienceStrip():void
		{
			blueExperienceStrip.x = playerManorLevelIcon.x + playerManorLevelIcon.width + 3;
			blueExperienceStrip.y = playerManorLevelIcon.y;
			_playerManorLevelTxt.y = playerManorLevelIcon.y;
			_playerBlueExperienceTxt.x = blueExperienceStrip.x + 25;
			_playerBlueExperienceTxt.y = blueExperienceStrip.y;
			uicommand.addChild(_playerLevelTxt);
			uicommand.addChild(_playerManorLevelTxt);
			uicommand.addChild(blueExperienceStrip);
			uicommand.addChild(_playerBlueExperienceTxt);
		}
		private function setPlayerManorLevelIcon():void
		{
			playerManorLevelIcon.x = 12;
			playerManorLevelIcon.y = 11 + playerExperiencePlane.height - 10;
			uicommand.addChild(playerManorLevelIcon);
		}
	}
}